/**
 * Copyright (c) 2018 软唐 All rights reserved.
 * <p>
 * https://www.softtown.cn
 * <p>
 * 版权所有，侵权必究！
 */

package com.softtown.modules.sys.controller;

import com.qcloud.cos.utils.StringUtils;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.AssertUtils;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.modules.security.service.ShiroService;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;


import com.softtown.modules.sys.dto.SysMenuDTO;
import com.softtown.modules.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 菜单管理
 *
 * @author Ding Ya Yong
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private ShiroService shiroService;

    @GetMapping("permissions")
    @ApiOperation("权限标识")
    public Result<Set<String>> permissions(@ApiIgnore @RequestParam Map<String, Object> params) {

        UserDetail user = SecurityUser.getUser();
        //用户权限列表
        Set<String> set = new HashSet<>();
        if(!"".equals(params.get("adminUserId")) && !"".equals(params.get("compId"))){
            Long adminUserId = Long.valueOf((String) params.get("adminUserId"));
            Long compId = Long.valueOf((String) params.get("compId"));
             set = shiroService.getUserPermission(user,adminUserId,compId);
        }else {
            set = shiroService.getUserPermissions(user);
        }


        return new Result<Set<String>>().ok(set);
    }

    @GetMapping("list")
    @ApiOperation("列表")
    @ApiImplicitParam(name = "type", value = "菜单类型 0：菜单 1：按钮  null：全部", paramType = "query", dataType = "int")
    @RequiresPermissions("sys:menu:list")
    public Result<List<SysMenuDTO>> list(Integer type) {
        List<SysMenuDTO> list = sysMenuService.getAllMenuList(type);

        return new Result<List<SysMenuDTO>>().ok(list);
    }

    @PostMapping("/getAppInfo")
    @ApiOperation("信息")
    public Result<SysMenuDTO> get(@RequestBody JSONObject param) {
        String menuId = param.get("appId").toString().trim();
        boolean isNullOrEmpty = StringUtils.isNullOrEmpty(menuId);
        Result result = new Result<>();
        if (isNullOrEmpty) {
            return result.error(ErrorCode.APP_ERROR);
        }
        if (!RegularUtils.isNum(menuId)) {
            return result.error(ErrorCode.INCORRECT_DATA_FORMAT);
        }
        SysMenuDTO data = sysMenuService.get(Long.parseLong(menuId));

        return new Result<SysMenuDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("sys:menu:save")
    public Result save(@RequestBody SysMenuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysMenuService.save(dto);

        return new Result<>();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:menu:update")
    public Result update(@RequestBody SysMenuDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysMenuService.update(dto);

        return new Result<>();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("sys:menu:delete")
    public Result delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id, "id");

        Result result = new Result<>();
        //判断是否有子菜单或按钮
        List<SysMenuDTO> list = sysMenuService.getListPid(id);
        if (list.size() > 0) {
            return result.error(ErrorCode.SUB_MENU_EXIST);
        }

        sysMenuService.delete(id);

        return result;
    }


}